﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Models;
using BLL;
using System.Net.Mail;
using System.Net;

namespace ShiCaiWang1
{
    public partial class C_LogIn : System.Web.UI.Page
    {
        private UserBLL ub = new UserBLL();
        private UserInfo user = new UserInfo();
        private int i;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }

        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            user.Account = op0.Text.ToString().Trim();
            user.Phone = op1.Text.ToString().Trim();
            user.Email = op4.Text.ToString().Trim();
            user.Pwd = op2.Text.ToString().Trim();
            user.UserName = op5.Text.ToString().Trim();
            DataTable dt = ub.GetUserList(user);
            if (dt != null)
            {
                Response.Write("<script>alert('注册失败')</script>");
                return;
            }
            if (ub.AddUser(user) == false)
            {
                Response.Write("<script>alert('注册失败')</script>");
                return;
            }
            if (op7.Text == user.EmailCode)
            {
                si7.Text = "邮箱验证码输入错误";
            }
            Response.Write("<script>alert('注册成功') ; location.href=C_SignIn.aspx';</script>");
        }

        protected void op1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            Random rad = new Random();//实例bai化随机数产生器rad；
            int value = rad.Next(1000, 10000);//用rad生成大于等于1000，小于等于9999的随机数；
            i = int.Parse(value.ToString());
            btn1.Text = i.ToString();
        }
        protected void btn3_Click(object sender, EventArgs e)
        {
            if (op6.Text == "")
            {
                si7.Text = "请输入验证码";
                return;
            }
            if (op6.Text != btn1.Text)
            {
                si7.Text = "验证码输入错误";
                return;
            }
            if (op4.Text == "")
            {
                si7.Text = "请输入邮箱";
                return;
            }
            if (op6.Text == btn1.Text)
            {
                //实例化一个发送邮件类。
                MailMessage mailMessage = new MailMessage();
                //发件人邮箱地址，方法重载不同，可以根据需求自行选择。
                mailMessage.From = new MailAddress("397076115@qq.com");
                //收件人邮箱地址。
                mailMessage.To.Add(new MailAddress(op4.Text.ToString().Trim()));
                //邮件标题。
                mailMessage.Subject = "这是你的验证码";
                string verificationcode = createrandom(6);
                user.EmailCode = verificationcode.ToString();
                //邮件内容。
                mailMessage.Body = "你的验证码是" + verificationcode;
                //实例化一个SmtpClient类。
                SmtpClient client = new SmtpClient();
                //在这里我使用的是qq邮箱，所以是smtp.qq.com，如果你使用的是126邮箱，那么就是smtp.126.com。
                client.Host = "smtp.qq.com";
                //使用安全加密连接。
                client.EnableSsl = true;
                //不和请求一块发送。
                client.UseDefaultCredentials = false;
                //验证发件人身份(发件人的邮箱，邮箱里的生成授权码);
                client.Credentials = new NetworkCredential("397076115@qq.com", "mpfelfsyxgaobgjc");
                //发送
                client.Send(mailMessage);
                //MessageBox.Show("发送成功！", "恭喜！");
                Response.Write("<script>alert('发送成功')</script>");
            }
        }
        private string createrandom(int codelengh)
        {
            int rep = 0;
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codelengh; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;

        }
    }
}